package com.example.meeting.controller;

import com.example.meeting.model.domain.Admin;
import com.example.meeting.model.responseData.CommonResult;
import com.example.meeting.service.IAdminService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class AdminController {
    @Autowired
    private IAdminService adminService;

    /**
     * 查询对应id的管理员
     * @param id
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/admin/{id}")
    public CommonResult selectAdminById(@PathVariable Long id){
        Admin admin = adminService.selectAdminById(id);
        if (admin == null){
            return CommonResult.fail("此管理员不存在");
        }
        return CommonResult.success(admin);
    }

    /**
     * 查询管理员列表
     * @param page
     * @param pageSize
     * @param admin
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/adminList")
    public CommonResult selectAdminList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                        @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                        @RequestBody Admin admin){
        PageInfo<Admin> adminList = adminService.selectAdminList(page, pageSize, admin);
        if (adminList == null) {
            return CommonResult.fail("管理员列表为空！");
        }
        return CommonResult.success(adminList);
    }

    /**
     * 新增普通管理员
     * @param admin
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/admin")
    public CommonResult addAdmin(@Validated @RequestBody Admin admin){
        Integer res = adminService.addAdmin(admin);
        if (res == 0){
            return CommonResult.fail("新增用户失败！");
        }
        return CommonResult.success(res);
    }

    /**
     * 修改管理员
     * @param admin
     * @return
     */
    @RequiresAuthentication
    @PutMapping("/admin")
    public CommonResult updateAdmin(@Validated @RequestBody Admin admin){
        Integer res = adminService.updateAdmin(admin);
        if (res == 0){
            return CommonResult.fail("修改管理员失败！");
        }
        return CommonResult.success(res);
    }

    /**
     * 删除管理员
     * @param id
     * @return
     */
    @RequiresAuthentication
    @DeleteMapping("/admin/{id}")
    public CommonResult deleteAdmin(@PathVariable Long id){
        return adminService.deleteAdmin(id);
    }

    /**
     * 修改管理员密码
     * @param jsonStr
     * @return
     */
    @RequiresAuthentication
    @PostMapping("/admin/updatePwd")
    public CommonResult updatePwd(@RequestBody String jsonStr){
        return adminService.updateAdminPassword(jsonStr);
    }
}
